
const MongoClient = require("mongodb").MongoClient;
const MongoDB = require("mongodb");
const UUID = require("uuid");
let lendData = require('./json/lend.json');
let userData = require('./json/user.json'); 
// const url =  "mongodb://用户名:密码@ip地址:27017/xxx?authSource=xxx";
const url =  "mongodb://175.178.84.167:27017";

insertData('lends',lendData);
insertData('users',userData);
// 插入数据 
function insertData(collName, data) {
  MongoClient.connect(url, async(err, db) => {
    if (err) {
      console.info("数据库创建失败!");
      db.close();
    } else {
      try {
        console.info("数据库已创建!");
        let dbo = db.db("finance");
        let delRes = await dbo.collection(collName).deleteMany({});
        console.info('删除结果',delRes);
        let insertRes = await dbo.collection(collName).insertMany(data);
        console.info('插入结果',insertRes);
        db.close();
      } catch (error) {
        console.error(error);
      }
    }
  });
} 